package com.lyh.hkcloud.base;

import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import com.lyh.hkcloud.R;

import java.lang.reflect.ParameterizedType;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public abstract class BaseEntityViewAdapter<K extends BaseViewHolder, V> extends BaseAdapter {
    protected List<V> list;
    protected Context context;

    protected ClickCallback callback;

    protected boolean enableManualSelection = false;

    public Map<String, Boolean> verifys = new HashMap<>();


    public BaseEntityViewAdapter(Context context, List<V> list) {
        this.list = list;
        this.context = context;
    }

    @Override
    public final int getCount() {
        return list.size();
    }

    @Override
    public final Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public final long getItemId(int position) {
        return position;
    }

    @Override
    public final View getView(int position, View convertView, ViewGroup parent) {
        K holder;
        V obj = list.get(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(
                    getItemResuId(), null);
            holder = getTag();
            initBaseItem(position, holder, convertView, obj);
            initItem(position, holder, convertView, obj);

            convertView.setTag(holder);
        } else {
            holder = (K) convertView.getTag();
        }

        setItem(position, holder, obj);
        initBaseEvent(holder, position);
        addEvent(position, holder, obj);
        return convertView;
    }

    private void initBaseItem(int position, K holder, View v, V obj) {
        // 复选框
        //holder.cbChecked = v.findViewById(R.id.cb_checked);
    }

    private void initBaseEvent(K holder, final int position) {
        if (holder.cbChecked != null && enableManualSelection && callback != null) {
            holder.cbChecked.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    callback.call(position, "checked", ((CheckBox) v).isChecked() ? "1" : "0");
                }
            });
        }
    }

    K getTag() {
        ParameterizedType superClass = (ParameterizedType) getClass().getGenericSuperclass();
        Class<K> type = (Class<K>) superClass.getActualTypeArguments()[0];
        try {
            return type.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    protected void confirm(String title, String msg, final BaseActivity.ConfirmListener listener) {
        new androidx.appcompat.app.AlertDialog.Builder(context).setTitle(title).setMessage(msg).setIcon(R.drawable.ask)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        listener.success();
                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                listener.cancel();
            }
        }).create().show();
    }

    abstract protected int getItemResuId();

    abstract protected void initItem(int position, K holder, View v, V obj);

    abstract protected void setItem(int position, K holder, V obj);

    protected void addEvent(int position, K holder, V obj) {

    }

    public interface ClickCallback {
        void call(int position, String key, String value);
        default void call(int position, String key, Object value){}
    }


}

